//
//  SelectInviterCell.m
//  PigMarket
//
//  Created by Felix on 2019/3/14.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "SelectInviterCell.h"

#define SelectInviterCellID @"SelectInviterCellID"

@interface SelectInviterCell ()

/**
 iconImageView
 */
@property (nonatomic , strong) UIImageView *iconImageView;

/**
 是否在线
 */
@property (nonatomic , strong) UIImageView *isOnLine;

/**
 用户类型
 */
@property (nonatomic , strong) UIImageView *userType;

/**
 userName
 */
@property (nonatomic , strong) UILabel *userName;

/**
 性别
 */
@property (nonatomic , strong) UIImageView *gender;

/**
 selectBtn
 */
@property (nonatomic , strong) UIButton *selectBtn;

/**
 备注
 */
@property (nonatomic , strong) UILabel *commentLabel;

/**
 邀请码
 */
@property (nonatomic , strong) UILabel *inviterCode;

/**
 距离
 */
@property (nonatomic , strong) UILabel *distance;
/**
 line
 */
@property (nonatomic , strong) UIView *line;
@end

@implementation SelectInviterCell

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    SelectInviterCell *cell = [tableView dequeueReusableCellWithIdentifier:SelectInviterCellID];
    if(!cell)
    {
        cell = [[SelectInviterCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SelectInviterCellID];
    }
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 关闭cell点击效果
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 0)];
        // 初始化UI
        [self initView];
        // 布局
        [self initLayout];
    }
    return self;
}
- (void)initView {
    [self.contentView addSubview:self.iconImageView];
    [self.contentView addSubview:self.isOnLine];
    [self.contentView addSubview:self.userType];
    [self.contentView addSubview:self.userName];
    [self.contentView addSubview:self.gender];
    [self.contentView addSubview:self.commentLabel];
    [self.contentView addSubview:self.inviterCode];
    [self.contentView addSubview:self.distance];
    [self.contentView addSubview:self.selectBtn];
    [self.contentView addSubview:self.line];
}

- (void) initLayout {
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(15);
        make.size.mas_equalTo(CGSizeMake(52, 52));
    }];
    [self.isOnLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.iconImageView.mas_right);
        make.bottom.mas_equalTo(self.iconImageView.mas_bottom);
    }];
    [self.userType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImageView.mas_right).offset(10);
        make.top.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(17, 17));
    }];
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userType.mas_right).offset(4);
        make.centerY.mas_equalTo(self.userType.mas_centerY);
    }];
    [self.userName setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.gender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userName.mas_right).offset(4);
        make.centerY.mas_equalTo(self.userType.mas_centerY);
    }];
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userType.mas_left);
        make.top.mas_equalTo(self.userType.mas_bottom).offset(10);
        make.right.mas_equalTo(-20);
    }];
    [self.inviterCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userType.mas_left);
        make.top.mas_equalTo(self.commentLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(18 * [JTool getScreenWidthScale]);
        make.bottom.mas_equalTo(-15);
    }];
    [self.distance mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.inviterCode.mas_right).offset(4);
        make.height.mas_equalTo(18 * [JTool getScreenWidthScale]);
        make.centerY.mas_equalTo(self.inviterCode.mas_centerY);
    }];
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.right.mas_equalTo(-20);
        make.size.mas_equalTo(CGSizeMake(56, 27       ));
    }];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userType.mas_left);
        make.height.mas_equalTo(1);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}
//选择他
- (void)selectInviterTapped {
    if (self.selectInviterBlock) {
        self.selectInviterBlock();
    }
}
#pragma mark - getter and setter

- (void)setSelectInviter:(SelectInviter *)selectInviter {
    _selectInviter = selectInviter;
    self.userName.text = selectInviter.name;
    self.inviterCode.text = [NSString stringWithFormat:@" 邀请码：%@ ",selectInviter.memberNum];
    self.commentLabel.text = selectInviter.remark;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:selectInviter.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_header"]];
    if (selectInviter.distance == 0) {
        self.distance.hidden = YES;
    }else {
        self.distance.hidden = NO;
        self.distance.text = [NSString stringWithFormat:@" %@ ",selectInviter.distance];
    }
    switch ([selectInviter.onlineIf intValue]) {//判断是否在线
        case OnLineTypeOn:
            self.isOnLine.image = [UIImage imageNamed:@"register_selectInviter_online"];
            break;
        case OnLineTypeOff:
            self.isOnLine.image = [UIImage imageNamed:@"register_selectInviter_offline"];
            break;
        default:
            break;
    }
    switch ([selectInviter.sex intValue]) {//性别
        case SexTypeMan:
            self.gender.image = [UIImage imageNamed:@"user_man"];
            break;
        case SexTypeWoman:
            self.gender.image = [UIImage imageNamed:@"user_woman"];
            break;
        default:
            break;
    }
    switch ([selectInviter.identity intValue]) {//会员 服务商  基金会
        case IdentityTypeVIP:
            self.userType.image = [UIImage imageNamed:@"register_selectInviter_putongyuan"];
            break;
        case IdentityTypeService:
            self.userType.image = [UIImage imageNamed:@"register_selectInviter_fuwushang"];
            break;
        case IdentityTypeFoundation:
            self.userType.image = [UIImage imageNamed:@"register_selectInviter_siyangyuan"];
            break;
        default:
            break;
    }
}

- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.image = [UIImage imageNamed:@"user_userinfo_defaulthead"];
        _iconImageView.layer.cornerRadius = 26;
        _iconImageView.layer.masksToBounds = YES;
    }
    return _iconImageView;
}
- (UIImageView *)isOnLine {
    if (!_isOnLine) {
        _isOnLine = [[UIImageView alloc] init];
    }
    return _isOnLine;
}
- (UIImageView *)userType {
    if (!_userType) {
        _userType = [[UIImageView alloc] init];
    }
    return _userType;
}
- (UILabel *)userName {
    if (!_userName) {
        _userName = [[UILabel alloc] init];
        _userName.textColor = [UIColor textBlackColor];
        _userName.font = [UIFont largerFont];
    }
    return _userName;
}
- (UIImageView *)gender {
    if (!_gender) {
        _gender = [[UIImageView alloc] init];
    }
    return _gender;
}
- (UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectBtn setTitle:@"选择ta" forState:UIControlStateNormal];
        _selectBtn.titleLabel.font = [UIFont smallFont];
        [_selectBtn setTitleColor:[UIColor primaryColor] forState:UIControlStateNormal];
        _selectBtn.layer.cornerRadius = 4;
        _selectBtn.layer.masksToBounds = YES;
        _selectBtn.layer.borderWidth = 1;
        _selectBtn.layer.borderColor = [UIColor primaryColor].CGColor;
        [_selectBtn addTarget:self action:@selector(selectInviterTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}
- (UILabel *)commentLabel {
    if (!_commentLabel) {
        _commentLabel = [[UILabel alloc] init];
        _commentLabel.font = [UIFont middleFont];
        _commentLabel.textColor = [UIColor textGrayColor];
       
    }
    return _commentLabel;
}
- (UILabel *)inviterCode {
    if (!_inviterCode) {
        _inviterCode = [[UILabel alloc] init];
        _inviterCode.textColor = [UIColor colorWithHexString:@"#9c9c9c"];
        _inviterCode.font = [UIFont systemFontOfSize:10];
        _inviterCode.layer.cornerRadius = 2;
        _inviterCode.layer.masksToBounds = YES;
        _inviterCode.layer.borderWidth = 0.5;
        _inviterCode.layer.borderColor = [UIColor colorWithHexString:@"#eeeeee"].CGColor;
       
    }
    return _inviterCode;
}
- (UILabel *)distance {
    if (!_distance) {
        _distance = [[UILabel alloc] init];
        _distance.textColor = [UIColor colorWithHexString:@"#9c9c9c"];
        _distance.font = [UIFont systemFontOfSize:10];
        _distance.layer.cornerRadius = 2;
        _distance.layer.masksToBounds = YES;
        _distance.layer.borderWidth = 0.5;
        _distance.layer.borderColor = [UIColor colorWithHexString:@"#eeeeee"].CGColor;
        
    }
    return _distance;
}
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    }
    return _line;
}
@end
